package com.arron.ssm.controller;

import com.arron.ssm.entity.Dept;
import com.arron.ssm.service.DeptService;
import com.arron.ssm.vo.ResultObjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname DeptController
 * @Date 2020/9/24 16:32
 * @Created by 刘
 */
@Controller
public class DeptController {

    @Autowired
    DeptService deptService;

    //查出所有的部门，并构造树型结构
    @GetMapping("/depts/tree")
    @ResponseBody
    public List<Dept> getDeptByTree(){
        List<Dept> depts = deptService.getAllDeptByTree();
        return depts;
    }

    //新增一个部门
    @PostMapping("/dept")
    @ResponseBody
    public ResultObjVo addDept(@RequestBody Dept dept){
        if (deptService.addDept(dept)>0){
            return new ResultObjVo(0,"添加成功");
        }else {
            return new ResultObjVo(-1,"添加失败");
        }
    }

    //删除一个部门
    @ResponseBody
    @DeleteMapping("/dept/{id}")
    public ResultObjVo deleteDept(@PathVariable("id") Long id){
        deptService.removeById(id);
        return new ResultObjVo(0,"删除成功");
    }

    //修改部门
    @PutMapping("/dept/{id}")
    @ResponseBody
    public ResultObjVo updateDept(@PathVariable("id") Integer id,@RequestBody Dept dept){
        if (deptService.update(dept,id)>0){
            return new ResultObjVo(0,"更新成功");
        }
        return new ResultObjVo(-1,"更新失败");
    }

}
